package cn.game.tanke.role;

import cn.game.tanke.panel.TankPanel;
import cn.game.tanke.util.TankMap;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;

public class Player extends Role implements KeyListener {
    private static Player player = new Player();
    private List<Bullet> bullets;
    private TankPanel panel;

    private Player() {
        super(TankMap.player, 28, 28, 0, 0, 11, 21);
        bullets = new ArrayList<>();
    }

    public static Player getInstance() {
        return player;
    }

    public void setPanel(TankPanel panel) {
        this.panel = panel;
    }

    @Override
    void annimal() {
        int step = getStep();
        if (step > 1) {
            setStep(0);
        } else {
            setStep(++step);
        }
    }

    @Override
    void move() {
        int x = getX();
        int y = getY();
        int dir = getDir();
        switch (dir) {
            case 0: // 上
                if (isCanMove(x, y - 1)) {
                    setY(y - 1);
                }
                break;
            case 2: // 下
                if (isCanMove(x, y + 1)) {
                    setY(y + 1);
                }
                break;
            case 3: // 左
                if (isCanMove(x - 1, y)) {
                    setX(x - 1);
                }
                break;
            case 1: // 右
                if (isCanMove(x + 1, y)) {
                    setX(x + 1);
                }
                break;
        }
    }

    @Override
    public void keyTyped(KeyEvent keyEvent) {
    }

    @Override
    public void keyPressed(KeyEvent keyEvent) {
        int dir = getDir();
        switch (keyEvent.getKeyCode()) {
            case KeyEvent.VK_UP:
                if (dir != 0) {
                    setDir(0);
                } else {
                    move();
                }
                break;
            case KeyEvent.VK_DOWN:
                if (dir != 2) {
                    setDir(2);
                } else {
                    move();
                }
                break;
            case KeyEvent.VK_LEFT:
                if (dir != 3) {
                    setDir(3);
                } else {
                    move();
                }
                break;
            case KeyEvent.VK_RIGHT:
                if (dir != 1) {
                    setDir(1);
                } else {
                    move();
                }
                break;
            case KeyEvent.VK_SPACE:
                shoot();
                break;
        }
    }

    private boolean isCanMove(int x, int y) {
        if (x < 0 || y < 0 || x >= TankMap.MAPARRAY[0].length || y >= TankMap.MAPARRAY.length) {
            return false;
        }
        int blockType = TankMap.MAPARRAY[y][x];
        // 禁止在砖墙、钢墙、冰墙、水域上移动
        if (blockType == 3 || blockType == 4 || blockType == 5|| blockType == 6) {
            return false;
        }
        return true;
    }

    @Override
    public void keyReleased(KeyEvent keyEvent) {
    }

    @Override
    public void drawRole(Graphics graphics, JPanel jPanel) {
        super.drawRole(graphics, jPanel);
        int x = getX();
        int y = getY();
        if (TankMap.MAPARRAY[y][x] == 2) {
            graphics.drawImage(TankMap.grass, x * 32 - 5, y * 32 - 5, (x + 1) * 32 + 5, (y + 1) * 32 + 5, 0, 0, 87, 83, jPanel);
        }
        // 绘制子弹
        for (int i = 0; i < bullets.size(); i++) {
            Bullet bullet = bullets.get(i);
            if (bullet.isGoOn()) {
                bullet.drawRole(graphics, jPanel);
            } else {
                bullets.remove(i);
                i--;
            }
        }
    }


    protected void shoot() {
        if (panel == null) return;

        int x = getX();
        int y = getY();
        int dir = getDir();

        // 根据方向调整子弹的初始位置
        switch (dir) {
            case 0: // 上
                y = y - 1;
                break;
            case 1: // 右
                x = x + 1;
                break;
            case 2: // 下
                y = y + 1;
                break;
            case 3: // 左
                x = x - 1;
                break;
        }

        // 创建子弹（0表示玩家子弹）
        Bullet bullet = new Bullet(0, x, y, 0, dir, panel);
        panel.getBulletManager().addBullet(bullet);
        bullets.add(bullet);
    }

    public List<Bullet> getBullets() {
        return bullets;
    }

    public void hit() {
        // 处理玩家被击中的逻辑
        System.out.println("玩家被击中！");
        // 可以添加减少生命值等逻辑
    }
}